/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectWindow.java
 *
 * Created on 05/05/2012, 17:11:40
 */

package forumgui;

import GuiClient.TelnetClient;
import TamirsGui.CreateMsgToServer;
import TamirsGui.GuiMsg;
import TamirsGui.ServerMsg;
import java.net.ConnectException;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Roy
 */
public class ConnectWindow extends javax.swing.JFrame {

    /** Creates new form ConnectWindow */
    public ConnectWindow() {
        initComponents();
        f= null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        connectLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        connectButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        IPTextFiled = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Forum");

        connectLabel.setText("please enter server IP");
        connectLabel.setName("connectLabel"); // NOI18N

        jLabel1.setText("IP:");
        jLabel1.setName("jLabel1"); // NOI18N

        connectButton.setText("connect");
        connectButton.setName("connectButton"); // NOI18N
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        exitButton.setText("exit");
        exitButton.setName("exitButton"); // NOI18N
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        IPTextFiled.setName("IPTextFiled");
        IPTextFiled.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                IPTextFiledKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(connectLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(IPTextFiled, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 44, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(connectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                        .addComponent(exitButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectLabel)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(IPTextFiled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectButton)
                    .addComponent(exitButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        // TODO add your handling code here:
        String ip = this.IPTextFiled.getText();
        try
        {
            TelnetClient client = new TelnetClient(ip, 61614);
            CreateMsgToServer protocol = new ServerMsg();
            f = new Forum(client,protocol);
             String viewForumMsg = protocol.getViewForumMsg();
            client.sendMessageToServer(viewForumMsg);
            this.setVisible(false);
            f.setVisible(true);
        }
        catch (Exception e)
        {
             JOptionPane.showMessageDialog(this,"connection Failed\nPlease try again","connection failed"
                   ,JOptionPane.ERROR_MESSAGE);
             e.printStackTrace();
        }
          catch (Throwable t)
        {
             JOptionPane.showMessageDialog(this,"connection Failed\nPlease try again" + t.getMessage(),"connection failed"
                   ,JOptionPane.ERROR_MESSAGE);
             t.printStackTrace();
        }
          

    }//GEN-LAST:event_connectButtonActionPerformed

    private void IPTextFiledKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_IPTextFiledKeyPressed
        if (evt.getKeyChar() == '\n')
        {
            this.connectButtonActionPerformed(null);
        }
    }//GEN-LAST:event_IPTextFiledKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConnectWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField IPTextFiled;
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel connectLabel;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    private static Forum f;
    
    private Vector<GuiMsg> getSubForum() {
        
        Vector<GuiMsg> ans = new Vector<GuiMsg>();
//        ans.add(new GuiMsg(null, "world", "hello", GuiMsg.MessageType.postMsg, true));
        return ans;
    }
    
    public static Forum getForum()
    {
        return f;
    }

}
